﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockPrjTeam2.Biz;
using MockPrjTeam2.Entity;
using MockPrjTeam2.Pages.Controls;

namespace MockPrjTeam2.Pages.Forms
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        private int trustRegionId;
        private TrustRegionBiz biz;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!int.TryParse(Request.Params["id"], out trustRegionId))
            {
                Response.Redirect("../Views/TrustRegionList.aspx");
            }
            else
            {
                biz = new TrustRegionBiz();

            }
            if (!IsPostBack)
            {
                GetTrustRegionById();
            }
            LoadTrustDistrictList();
        }

        private void GetCountry(int countryId)
        {

            var countryBiz = new CountryBiz();
            DropDownListCountry.DataSource = countryBiz.GetAllCountry();
            DropDownListCountry.DataTextField = "CountryName";
            DropDownListCountry.DataValueField = "CountryID";
            DropDownListCountry.DataBind();
            DropDownListCountry.SelectedValue = countryId.ToString(CultureInfo.InvariantCulture);
        }

        private void GetTrustRegionById()
        {

            TrustRegion trustRegion = biz.GetById(trustRegionId);

            TxtTrustRegionName.Text = trustRegion.TrustRegionName;
            Session["TrustRegionName"] = trustRegion.TrustRegionName;
            Session["TrustRegionID"] = trustRegion.TrustRegionID;
            TxtDescription.Text = trustRegion.Description;
            this.GetCountry(trustRegion.CountryID);
            HiddenFieldTrustRegionId.Value = trustRegion.TrustRegionID.ToString();
        }

        public int CheckTrustRegionNameExist(string trustRegionName)
        {
            TrustRegionBiz trustRegionBiz = new TrustRegionBiz();
            return trustRegionBiz.CheckNameExist(trustRegionName);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            {
                TrustRegionBiz biz = new TrustRegionBiz();
                TrustRegion trustRegion = new TrustRegion();
                int checkTrustRegionName = CheckTrustRegionNameExist(TxtTrustRegionName.Text);
                if (checkTrustRegionName > 0)
                {
                    lblCheckTrustRegionName.Text = "Trust Region Name does exist ! Please new Trust Region Name !";
                    return;
                }
                trustRegion.TrustRegionID = trustRegionId;
                trustRegion.TrustRegionName = TxtTrustRegionName.Text;
                trustRegion.Description = TxtDescription.Text;
                trustRegion.CountryID = Convert.ToInt32(DropDownListCountry.SelectedValue);

                bool result = biz.UpdateTrustRegion(trustRegion);
                if (result == true)
                {
                    Response.Redirect("../Views/TrustRegionList.aspx");
                }
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("../Views/TrustRegionList.aspx");
        }
        private void LoadTrustDistrictList()
        {

            TrustDistrictPanel.Visible = true;
            LinkTrustDistrict.Visible = true;

            var trustDistrictList =
                (TrustDistrictList)Page.LoadControl("~/Pages/Controls/TrustDistrictList.ascx");

            HolderDirectorate.Controls.Clear();
            HolderDirectorate.Controls.Add(trustDistrictList);
        }
        protected void ButtonInactive_Command(object sender, CommandEventArgs e)
        {
            int trustRegionId;
            if (int.TryParse(HiddenFieldTrustRegionId.Value, out trustRegionId))
            {
                TrustRegionBiz trustRegionBiz = new TrustRegionBiz();
                trustRegionBiz.SetInActive(trustRegionId, false);
                ButtonInactive.Visible = false;
            }
        }
    }
}